﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using sharp_net;
using sharp_net.Repositories;
using System.Configuration;
using zkdao.Enum;

namespace zkdao.Domain {

    public class User : DomainGuid, IAggregateRoot {

        public int RoleEnum { get; set; }
        public eRole Role {
            get { return (eRole)Role; }
        }

        [Required]
        [MaxLength(100)]
        public string Email { get; set; }

        [Required]
        [Column("Password")]
        [MaxLength(128)]
        public string PasswordHash { get; set; }

        public static string GetHashPassword(string value) {
            return Encrypt.EncryptUserPassword(value);
        }

        [MaxLength(50)]
        public string Name { get; set; }

        [MaxLength(100)]
        public string Link { get; set; }

        [MaxLength(50)]
        public string QQ { get; set; }

        [MaxLength(50)]
        public string Weibo { get; set; }

        public DateTime? DateLastLogin { get; set; }
        public DateTime? DateLastPasswordChange { get; set; }

        public string ApprovedID { get; set; }

        public virtual ICollection<UserRelaRebate> RelaRebates { get; set; }

        //记录ID数组，减少系统负载。超出长度后截掉旧数据。如果使用Reply表记录UserID数组会增大查询数据量
        public string PlusRebateReplyIDs { get; set; }
        public string PlusInfoReplyIDs { get; set; }
        public string MinusRebateReplyIDs { get; set; }
        public string MinusInfoReplyIDs { get; set; }
        //public virtual ICollection<RebateReply> PlusRebateReplys { get; set; }
        //public virtual ICollection<RebateReply> MinusRebateReplys { get; set; }
        //public virtual ICollection<InfoReply> PlusInfoReplys { get; set; }
        //public virtual ICollection<InfoReply> MinusInfoReplys { get; set; }

        public void Register() {
            this.Creat();
            this.Act = eAct.unApproved;
            this.RequestApproved();
        }

        public void RequestApproved() {
            IEmailService emalimple = IocLocator.Instance.GetImple<IEmailService>();
            this.ApprovedID = Guid.NewGuid().ToString();
            emalimple.SendEmail(this.Email, "激活您的邮箱 - ZKDAO",
                "<a href='" + ConfigurationManager.AppSettings["WebDomain"] + "Account/Approved/" + this.Email + "?approvedID=" + this.ApprovedID + "' >点击即可激活zkdao帐号 -></a>", true);
        }

        public bool ValiApproved(string approvedID) {
            if (approvedID == this.ApprovedID) {
                this.ActEnum = (int)eAct.Normal;
                return true;
            } else {
                return false;
            }
        }
        public void Update(UserData data) {
            if (!string.IsNullOrEmpty(data.PasswordHash))
                PasswordHash = data.PasswordHash;
            if (!string.IsNullOrEmpty(data.Name))
                Name = data.Name;
            if (!string.IsNullOrEmpty(data.Link))
                Link = data.Link;
            if (!string.IsNullOrEmpty(data.QQ))
                QQ = data.QQ;
            if (!string.IsNullOrEmpty(data.Weibo))
                Weibo = data.Weibo;
            if (data.RoleEnum != 0)
                RoleEnum = data.RoleEnum;
            if (data.ActEnum != 0)
                ActEnum = data.ActEnum;
            if (data.DateLastLogin.HasValue)
                DateLastLogin = data.DateLastLogin;
            if (data.DateLastPasswordChange.HasValue)
                DateLastPasswordChange = data.DateLastPasswordChange;
        }
    }

    public class UserData: DomainGuidData {
        public int RoleEnum { get; set; }

        public string Email { get; set; }

        public string PasswordHash { get; set; }

        public string Name { get; set; }

        public string Link { get; set; }

        public string QQ { get; set; }

        public string Weibo { get; set; }

        public DateTime? DateLastLogin { get; set; }

        public DateTime? DateLastPasswordChange { get; set; }

        public string ApprovedID { get; set; }

        public virtual ICollection<UserRelaRebateData> RelaRebates { get; set; }

        public string PlusRebateReplyIDs { get; set; }
        public string PlusInfoReplyIDs { get; set; }
        public string MinusRebateReplyIDs { get; set; }
        public string MinusInfoReplyIDs { get; set; }
    }
}